package com.cloud.point.concurrent.disruptor;

import com.lmax.disruptor.RingBuffer;

import java.nio.ByteBuffer;
import java.util.concurrent.TimeUnit;

import lombok.extern.slf4j.Slf4j;

/**
 * LongEventProducer
 *
 * @author 24373
 * @version 1.0
 * @date 2024-06-27 22:43
 */
@Slf4j
public class LongEventProducer {

    public final RingBuffer<LongEvent> ringBuffer;

    public LongEventProducer(RingBuffer<LongEvent> ringBuffer) {
        this.ringBuffer = ringBuffer;
    }

    public void onData(ByteBuffer byteBuffer) {
        long sequence = ringBuffer.next();
        Long data = null;
        try {
            LongEvent longEvent = ringBuffer.get(sequence);
            data = byteBuffer.getLong(0);
            longEvent.setValue(data);
            TimeUnit.MILLISECONDS.sleep(10);
        } catch (InterruptedException e) {
            log.warn("{}", e);
        } finally {
            log.info("生产者准备发送数据，sequence:{}，data:{}", sequence, data);
            ringBuffer.publish(sequence);
        }

    }


}
